package yunwo.cn.yzstation.view.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Request;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.MyBaseActivity;
import yunwo.cn.yzstation.base.Yzapp;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.utils.StringUtil;

/**
 * Created by lao on 2017/9/1.
 */
public class ModifyPasswdActivity extends MyBaseActivity implements View.OnClickListener {
    private boolean isPasswordControlClicked = false;
    private boolean isPasswordControlClicked1 = false;
    private ImageView mPasswordControlTV1;
    private ImageView mPasswordControlTV;
    private EditText mOldpasw;
    private EditText mNewpasw;
    private Button mConfirm;
    private ProgressDialog mMyDialog;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        iniView();

    }

    private void iniView() {
        mPasswordControlTV1 = (ImageView) findViewById(R.id.passwordControlTV1);
        mPasswordControlTV = (ImageView) findViewById(R.id.passwordControlTV);
        mConfirm = (Button) findViewById(R.id.confirm);
        mOldpasw = (EditText) findViewById(R.id.oldpasw);
        mNewpasw = (EditText) findViewById(R.id.newpasw);
        mConfirm.setOnClickListener(this);
        mPasswordControlTV.setOnClickListener(this);
        mPasswordControlTV1.setOnClickListener(this);

    }

    @Override
    protected String settiltile() {
        return "密码修改";
    }

    @Override
    protected int getLayoutRsid() {
        return R.layout.activity_modifypassd;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.passwordControlTV:
                isPasswordControlClicked = !isPasswordControlClicked;
                if (isPasswordControlClicked) {
                    //true 明文
                    mPasswordControlTV.setImageResource(R.drawable.register_pwd2_02);
                    mOldpasw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mOldpasw.setSelection(mOldpasw.length());
                } else {
                    //false 密文
                    mOldpasw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    mPasswordControlTV.setImageResource(R.drawable.register_pwd2_01);
                    mOldpasw.setSelection(mOldpasw.length());
                }
                break;
            case R.id.passwordControlTV1:
                isPasswordControlClicked1 = !isPasswordControlClicked1;
                if (isPasswordControlClicked1) {
                    //true 明文
                    mPasswordControlTV1.setImageResource(R.drawable.register_pwd2_02);
                    mNewpasw.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mNewpasw.setSelection(mNewpasw.length());
                } else {
                    //false 密文
                    mNewpasw.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    mPasswordControlTV1.setImageResource(R.drawable.register_pwd2_01);
                    mNewpasw.setSelection(mNewpasw.length());
                }
                break;
            case R.id.confirm:
                if (StringUtil.isEmpty(mOldpasw.getText().toString().trim())) {
                    Toast.makeText(ModifyPasswdActivity.this, "输入旧密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUtil.isEmpty(mNewpasw.getText().toString().trim())) {
                    Toast.makeText(ModifyPasswdActivity.this, "输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                confirm();
                break;
        }
    }

    private void confirm() {
        Map<String, String> map = new HashMap<>();
        map.put("mobile", Yzapp.getInstance().getmobile());
        map.put("oldPassword", mOldpasw.getText().toString().trim());
        map.put("newPassword", mNewpasw.getText().toString().trim());
        HttpManager.hpptPost(MyConstants.userpassword, map).execute(new GLCallBack() {
            @Override
            public void onResponseT(String result, int code, String msg, int id) {
                if (code == 200) {
                    Toast.makeText(ModifyPasswdActivity.this, "修改成功,请重新登录", Toast.LENGTH_SHORT).show();
                    Intent intent =  new Intent(ModifyPasswdActivity.this, LoginActivity.class);
                    intent.putExtra("mobile", Yzapp.getInstance().getmobile());
                    startActivity(intent);
                    finish();
                }
                else {
                    Toast.makeText(ModifyPasswdActivity.this, msg, Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onError(Call call, Exception e, int id) {
                Toast.makeText(ModifyPasswdActivity.this, "服务器出错啦~", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBefore(Request request, int id) {

                if (mMyDialog == null) {
                    mMyDialog = ProgressDialog.show(ModifyPasswdActivity.this, null, "请稍等...");
                    mMyDialog.setCancelable(false);
                    mMyDialog.show();
                }
            }

            @Override
            public void onAfter(int id) {
                if (mMyDialog != null && mMyDialog.isShowing()) {
                    mMyDialog.dismiss();
                }

            }
        });
    }
}
